#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

#define BUFSIZE	1024

int main(int argc, char *argv[])
{
    DIR *dp = NULL; // 目录流 
    struct dirent *entry = NULL; // 目录项
    char buf[BUFSIZE] = {0};

    if (argc < 2)
        return 1;

    dp = opendir(argv[1]); // 打开目录返回目录流

    if (NULL == dp) {
        perror("opendir()");
        return 1;
    }

    while (1) {
        entry = readdir(dp); // 读取目录下的目录项

        if (NULL == entry) {
            if (errno) {
                perror("readdir()");
                goto ERROR;
            }
            break;
        }

        memset(buf, '\0', BUFSIZE);
        strcpy(buf, argv[1]);
        strcat(buf, "/");
        strcat(buf, entry->d_name);
        printf("%s\n", buf); // 输出文件的绝对路径
    }

    closedir(dp);

    return 0;
ERROR:
    closedir(dp);
    return 1;
}


